$(document).ready(function(){

    /***  商户列表  ***/
    var timeFlag = true
    var priceFlag = true
    var type = 1;
    var like = ''
    $('.sortItemAre').on('click',function(){
        var sortType = $(this).data('type')
        // console.log(timeFlag)
        // console.log(priceFlag)

        $('#timeImg').attr('src','../../img/community-service/up.png')
        $('#priceImg').attr('src','../../img/community-service/up.png')
        $('.timeTxt').css('color','black')
        $('.priceTxt').css('color','black')
        if(sortType == 'time'){
            $('.timeTxt').css('color','#0b67ff')
            if(timeFlag == true){
                timeFlag = false
                $('#timeImg').attr('src','../../img/community-service/downBlue.png')
                type = 2
                getGoodsList(goodsPageNo,showNum,type,like);
            }else{
                timeFlag = true
                $('#timeImg').attr('src','../../img/community-service/upBlue.png')
                type = 1
                getGoodsList(goodsPageNo,showNum,type,like);
            }
        }else if(sortType == 'price'){
            $('.priceTxt').css('color','#0b67ff')
            if(priceFlag == true){
                priceFlag = false
                $('#priceImg').attr('src','../../img/community-service/downBlue.png')
                type = 4
                getGoodsList(goodsPageNo,showNum,type,like);
            }else{
                priceFlag = true
                $('#priceImg').attr('src','../../img/community-service/upBlue.png')
                type = 3
                getGoodsList(goodsPageNo,showNum,type,like);
            }
        }
    })

    $('#searchTxt').on('click',function(){
        like = $('.search-box').val();
        getGoodsList(goodsPageNo,showNum,type,like);
    })


    /** 添加商品 **/
    function setShopImgUrl(res){
        document.getElementById('goods-img-url').value = res;
    }
    imgUpload('goods-img-select','goods-img-show',setShopImgUrl);


    // var imgurl;
    // var imgs = [];  //商品head展示图片
    // var imgArr = imgUploadShow('content-img-select','content-img-show');
    // //新图删除
    // $('#content-img-show').on('click','.cancle',function(){
    //     var index = $(this).parent().index();
    //     imgArr.splice(index,1);
    //     var contentImgList = document.getElementById('content-img-show');
    //     // // console.log(contentImgList);
    //     var contentNewImgList = contentImgList.getElementsByClassName('new')[0];
    //     var contentImgItem = document.getElementsByClassName('content-img-item');
    //     // // console.log(contentImgItem);
    //     contentNewImgList.removeChild(contentImgItem[index])
    //
    // })
    // //旧图删除
    // $('#content-img-show').on('click','.del',function(){
    //     var index = $(this).parent().index();
    //     // console.log(index);
    //     imgs.splice(index,1);
    //     // console.log(imgs)
    //     var contentImgList = document.getElementById('content-img-show');
    //     // // console.log(contentImgList);
    //     var contentNewImgList = contentImgList.getElementsByClassName('old')[0];
    //     var contentImgItem = document.getElementsByClassName('content-old-img-item');
    //     // // console.log(contentImgItem);
    //     contentNewImgList.removeChild(contentImgItem[index])
    //     // // console.log(imgs);
    // })
    // $('#upload-content-img').on('click',function(){
    //     allImgUpload(imgArr,setContentImgUrl)
    // });
    // function setContentImgUrl(res){
    //     // console.log(res)
    //     if(res.length != 0){
    //         imgurl = {
    //             banner_img : res
    //         } ;
    //         imgs.push(imgurl)
    //         // console.log(imgs);
    //         $('#upload-content-img').css('display','none');
    //     }
    // }


    // var imgurl2;
    // var imgs2 = [];  //商品head展示图片
    // var imgArr2 = imgUploadShow('content-img-select2','content-img-show2');
    // //新图删除
    // $('#content-img-show2').on('click','.cancle',function(){
    //     var index = $(this).parent().index();
    //     imgArr.splice(index,1);
    //     var contentImgList = document.getElementById('content-img-show2');
    //     var contentNewImgList = contentImgList.getElementsByClassName('new')[0];
    //     var contentImgItem = contentNewImgList.getElementsByClassName('content-img-item');
    //     contentNewImgList.removeChild(contentImgItem[index])
    //
    // })
    // //旧图删除
    // $('#content-img-show2').on('click','.del',function(){
    //     var index = $(this).parent().index();
    //     imgs2.splice(index,1);
    //     var contentImgList = document.getElementById('content-img-show2');
    //     var contentNewImgList = contentImgList.getElementsByClassName('old')[0];
    //     var contentImgItem = contentNewImgList.getElementsByClassName('content-old-img-item2');
    //     contentNewImgList.removeChild(contentImgItem[index])
    // })
    // $('#upload-content-img2').on('click',function(){
    //     allImgUpload(imgArr2,setContentImgUrl2)
    // });
    // function setContentImgUrl2(res){
    //     // console.log(res)
    //     if(res.length != 0){
    //         imgurl2 = {
    //             url:res
    //         } ;
    //         imgs2.push(imgurl2)
    //         // console.log(imgs2);
    //         $('#upload-content-img2').css('display','none');
    //     }
    // }


    //全局对象
    let page = {
        p:1,
        num:10,
        no_data:'暂无商品'
    }
    //页面初始化
    function init(){
        //获取商品列表
        getProductList(page.p,page.num);
    }
    init();


    //获取商品列表
    function getProductList(p,num){
        getAjax(`${global.apiUrl}/get_als_product_list`,res=>{
            console.log(res);
            let list = res.list;
            let html = '';
            page.product_list = res.list;
            if(res.list.length==0){
                $('#page').paging({
                    totalPage:0,
                    totalSize:0,
                    pageNo:0,
                })
                html += `
                    <tr>
                        <td colspan="5" style="text-align: center;">${page.no_data}</td>
                    </tr>   
                `
            }else{
                $('#page').paging({
                    totalPage:Math.ceil(res.count/num),
                    totalSize:res.count,
                    pageNo:p,
                    callback(pages){
                        page.p = pages;
                        getProductList(pages,page.num)
                    }
                })
                for(let i in list){
                    html += `
                    <tr>
                        <td class="product_img">
                            <img src="${list[i].head_img}" alt="">
                        </td>
                        <td>
                            ${list[i].product_name}
                        </td>
                        <td>${list[i].sub_content}</td>
                        <td>${list[i].price}</td>
                        <td class="control-box">
                            <span data-toggle="modal" data-target="#GoodsModal" data-type="2" data-id="${list[i].id}" class="reset_btn">修改</span>
                            <span data-id="${list[i].id}" class="product_del">删除</span>
                        </td>
                    </tr>
                `
                }
            }
            $('#goodsList').html(html);
        },{
            p:p,
            num:num,
            community_shop_id:0
        })
    }


    //初始化modal
    $('#GoodsModal').on('show.bs.modal',function(event){
        let modal = $(this);
        let btnThis = $(event.relatedTarget);
        let type = btnThis.data('type');//1添加   2修改
        console.log(type);
        modal.find('#operation_type').val(type);
        let product_id = modal.find('#product_id');
        product_id.val('');
        let product_name = modal.find('#goodsName');
        product_name.val('');
        let price = modal.find('#goodsPrice');
        price.val('');
        let danwei = modal.find('#goodsDw');
        danwei.val('');
        let guige = modal.find('#goodsGg');
        guige.val('');
        let sub_content = modal.find('#goodsDesc');
        sub_content.val('');
        let head_img = modal.find('#goods-img-url');
        head_img.val('');
        let show_html = '<div>图片预览</div>';
        modal.find('#goods-img-show').html(show_html);
        let modal_title = '';
        if(type==1){
            modal_title = '添加商品';
        }
        if(type==2){
            modal_title = '修改商品';
            let id = btnThis.data('id');
            modal.find('#product_id').val(id);
            let product_info = {};
            for(let i in page.product_list){
                if(id == page.product_list[i].id){
                    product_info = page.product_list[i];
                }
            }
            product_name.val(product_info.product_name);
            price.val(product_info.price);
            danwei.val(product_info.danwei);
            guige.val(product_info.guige);
            sub_content.val(product_info.sub_content);
            head_img.val(product_info.head_img);
            show_html = `
                <img src="${product_info.head_img}" />
            `
            modal.find('#goods-img-show').html(show_html);
        }
        modal.find('#GoodsModalLabel').text(modal_title);
    })
    //提交按钮
    $('#submit_btn').on('click',function(){
        console.log('submit_btn');
        let type = $('#operation_type').val();
        let id = $('#product_id').val();
        let product_name = $('#goodsName').val();
        let price = $('#goodsPrice').val();
        let danwei = $('#goodsDw').val();
        let guige = $('#goodsGg').val();
        let sub_content = $('#goodsDesc').val();
        let head_img = $('#goods-img-url').val();
        let community_shop_id = global.community_id;
        if(!community_shop_id){
            community_shop_id = 0;
        }
        console.log(community_shop_id);
        let pro = {
            id:id,
            product_name:product_name,
            price:price,
            head_img:head_img,
            guige:guige,
            sub_content:sub_content,
            danwei:danwei,
            community_shop_id:community_shop_id
        }
        pro = JSON.stringify(pro);
        operationProduct(type,pro);
    });

    //删除商品
    $('#goodsList').on('click','.product_del',function(){
        let id = $(this).data('id');
        console.log(id);
        operationProduct(3,null,id);
    })
    //操作商品
    function operationProduct(type,pro,id){
        console.log('operation');
        let text = type==1?'添加商品成功！':(type==2?'修改商品成功！':'商品已删除');
        if(type == 3){
            let rf = confirm('确人删除该商品？');
            if(rf){

            }else{
                return;
            }
        }
        getAjax(`${global.apiUrl}/operation_als_product`,res=>{
            console.log(res);
            if(res == 1){
                alert(text);
                $('#GoodsModal').modal('hide');
                getProductList(page.p,page.num);
            }
        },{
            type:type,
            pro:pro,
            id:id
        })
    }




    //添加参数
    var index = 0
    $('#parameterBtn').on('click',function(){
        index ++
        var html = `
                    <div class="parameterItem">
                        <div class="parameter-group">
                            <label for="parameter-name">名称</label>
                            <input type="text" name="name" class="parameter-name">
                        </div>
                        <div class="parameter-group">
                            <label for="parameter-value">内容</label>
                            <input type="text" name="value" class="parameter-value">
                        </div>
                    </div>
        `

        $('.parameterItemAre').append(html)
        if(index > 0){
            $('#parameterDelBtn').css('display','block')
        }
    })

    //删除
    $('#parameterDelBtn').on('click',function(){
        index --
        $('.parameterItemAre').children('.parameterItem').eq(index).remove()
        if(index == 0){
            $('#parameterDelBtn').css('display','none')
        }
    })






});
